/*
 * @(#)MCCIMT000200UV01Transformer.java
 * Date 2013-08-24
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.hl7v3common.transformer;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import org.hl7.v3.CommunicationFunctionType;
import org.hl7.v3.EntityClassDevice;
import org.hl7.v3.II;
import org.hl7.v3.MCCIMT000100UV01Sender;
import org.hl7.v3.MCCIMT000200UV01Acknowledgement;
import org.hl7.v3.MCCIMT000200UV01Device;
import org.hl7.v3.MCCIMT000200UV01Receiver;
import org.hl7.v3.MCCIMT000200UV01Sender;
import org.hl7.v3.MCCIMT000200UV01TargetMessage;
import org.hl7.v3.PRPAIN201301UV02;
import us.hornerscorners.lamppost.config.IServiceConfig;

import us.hornerscorners.lamppost.hl7v3common.constant.AckResponse;

import us.hornerscorners.lamppost.hl7v3common.constant.DeterminerCode;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 *
 * @author jim
 */
@ApplicationScoped
public class MCCIMT000200UV01Transformer
        extends AbstractTransformer<IServiceConfig, MCCIMT000200UV01Sender> {

    /** Field description */
    @Inject
    private CSTransformer csTransformer;

    /** Field description */
    @Inject
    private ENTransformer enTransformer;

    /** Field description */
    @Inject
    private IITransformer iiTransformer;

    /**
     * Constructs ...
     *
     */
    public MCCIMT000200UV01Transformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public MCCIMT000200UV01Receiver createMCCIMT000200UV01Receiver() {

        MCCIMT000200UV01Receiver result = new MCCIMT000200UV01Receiver();

        result.setTypeCode(CommunicationFunctionType.RCV);

        MCCIMT000200UV01Device device = new MCCIMT000200UV01Device();

        device.setClassCode(EntityClassDevice.DEV);
        device.setDeterminerCode(DeterminerCode.Instance.getCode());
        device.getId().add(this.iiTransformer.createNullFlavorII());
        result.setDevice(device);

        return result;
    }

    /**
     * Method description
     *
     *
     * @param config
     *
     * @return
     */
    @Override
    public MCCIMT000200UV01Sender transform(IServiceConfig config) {

        MCCIMT000200UV01Sender result = new MCCIMT000200UV01Sender();

        result.setTypeCode(CommunicationFunctionType.SND);

        MCCIMT000200UV01Device senderDevice = new MCCIMT000200UV01Device();

        senderDevice.setClassCode(EntityClassDevice.DEV);
        senderDevice.setDeterminerCode(DeterminerCode.Instance.getCode());
        senderDevice.getId().add(this.iiTransformer.transform(null, config.getId()));
        senderDevice.getName().add(this.enTransformer.transform(config.getName()));
        result.setDevice(senderDevice);

        return result;
    }

    /**
     * Method description
     *
     *
     * @param origSender
     *
     * @return
     */
    public MCCIMT000200UV01Receiver transform(MCCIMT000100UV01Sender origSender) {

        MCCIMT000200UV01Receiver result = new MCCIMT000200UV01Receiver();

        result.setTypeCode(CommunicationFunctionType.RCV);

        MCCIMT000200UV01Device device = new MCCIMT000200UV01Device();

        device.setClassCode(EntityClassDevice.DEV);
        device.setDeterminerCode(DeterminerCode.Instance.getCode());

        device.getId().addAll(origSender.getDevice().getId());
        device.getName().addAll(origSender.getDevice().getName());

        result.setDevice(device);

        return result;
    }

    /**
     * Method description
     *
     *
     * @param prpa301
     *
     * @return
     */
    public MCCIMT000200UV01Receiver transform(PRPAIN201301UV02 prpa301) {

        MCCIMT000200UV01Receiver result = null;

        MCCIMT000100UV01Sender origSender = prpa301.getSender();

        if (origSender == null) {

            result = createMCCIMT000200UV01Receiver();

        } else {

            result = transform(origSender);
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param prpa301
     * @param ackCode
     *
     * @return
     */
    public MCCIMT000200UV01Acknowledgement transform(PRPAIN201301UV02 prpa301,
            AckResponse ackCode) {

        MCCIMT000200UV01Acknowledgement result = new MCCIMT000200UV01Acknowledgement();

        result.setTypeCode(this.csTransformer.transform(ackCode));

        II targetMessage = prpa301.getId();

        if (targetMessage != null) {

            MCCIMT000200UV01TargetMessage targetMsg = new MCCIMT000200UV01TargetMessage();

            targetMsg.setId(targetMessage);
            result.setTargetMessage(targetMsg);
        }

        return result;
    }
}
